package com.tao.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.tao.gulimall.product.entity.AttrEntity;
import com.tao.gulimall.product.service.AttrService;
import com.tao.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.Query;

import com.tao.gulimall.product.dao.AttrGroupDao;
import com.tao.gulimall.product.entity.AttrGroupEntity;
import com.tao.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 分页查询, 基于分类id
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> lqw = new LambdaQueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(key)){
            lqw.eq(!StringUtils.isNullOrEmpty(key), AttrGroupEntity::getAttrGroupId, key)
                    .or()
                    .like(!StringUtils.isNullOrEmpty(key), AttrGroupEntity::getAttrGroupName, key);
        }
        // 分页查询信息
        IPage<AttrGroupEntity> page;
        if(catelogId == 0){
            // 查询所有
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    lqw
            );
        }else {
            lqw.eq(AttrGroupEntity::getCatelogId, catelogId);
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    lqw
            );
        }
        return new PageUtils(page);
    }


    /**
     * 根据分类id查询所有属性分组和分组下的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long catelogId) {

        // 查询所有属性分组
        LambdaQueryWrapper<AttrGroupEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AttrGroupEntity::getCatelogId, catelogId);
        List<AttrGroupEntity> groupEntities = this.list(lqw);
        // 查询每个属性分组下的所有属性
        List<AttrGroupWithAttrsVo> withAttrsVos = groupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, vo);
            List<AttrEntity> relaAttr = attrService.getRelaAttr(item.getAttrGroupId());
            vo.setAttrs(relaAttr);
            return vo;
        }).collect(Collectors.toList());

        return withAttrsVos;
    }

}